﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Browser;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Lib.Helper;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using MSIACCSL2.Views.SubGroup;
using WcfService1.DTO;

namespace MSIACCSL2.Views.SalesOrder
{
    public partial class FrmAdd : Page
    {
        public SalesOrderWrapViewModel SOW
        {
            get
            {
                return this.Resources["POW"] as SalesOrderWrapViewModel;
            }
        }

        public SalesOrderViewModel ViewModel
        {
            get
            {
                return this.DataContext as SalesOrderViewModel;
            }
        }

        private TextBox TxtSubGroupName;
        private TextBox TxtItemName;
        private TextBox TxtNoLog;
        private SearchSubgroup sg;
        private SearchItem si;

        public FrmAdd()
        {
            InitializeComponent();
            System.Net.WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

            this.Loaded += (x, y) =>
            {
                (this.Resources["POW"] as SalesOrderWrapViewModel).SalesOrderViewModel =
                     (SalesOrderViewModel)this.DataContext;
            };

            EventAggregator.RegisterMessage("Refresh",(x)=>
                                                          {
                                                              this.DataContext = x;
                                                          });
       
           
          this.Loaded += (x, y) =>
                               {
                                   rbSubGroup.Focus();
                               };

            this.KeyDown += (x, y) =>
                                {
                                    if (y.Key == Key.T && Keyboard.Modifiers == ModifierKeys.Control)
                                        SOW.AddSalesOrderDetail(null);
                                    else if (y.Key == Key.D && Keyboard.Modifiers == ModifierKeys.Control)
                                        SOW.DeleteSalesOrderDetail(null);
                                    else if (y.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                                        ViewModel.SaveSalesOrder(ViewModel.SalesOrder);
                                    else if (y.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                                        ViewModel.Cancel(null);
                                };
        }

    

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                SOW.GetInventory(((TextBox) sender).DataContext);
        }

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	(sender as TextBox).SelectAll();
        }


        //Lookup Subgroup
        private void KeyDo_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
        	if (e.Key == Key.Enter)
        	{
        	    sg = new SearchSubgroup();
                sg.Closed +=new EventHandler(sg_Closed);
                sg.txtSearch.GotFocus += (x, y) =>
                {
                    (x as TextBox).Text = (sender as TextBox).Text;
                    (x as TextBox).SelectAll();
                };
                sg.Show();
        	}

        }

        //Search Subgroup Finished
        private void sg_Closed(object sender, EventArgs e)
        {
            var Subgroup = (sender as SearchSubgroup).dg1.SelectedItem as SubGroupDTO;
            var soDetail = GridSubgroup.SelectedItem as SalesOrderDetailDTO;
            soDetail.SubGroup = Subgroup;
            GridSubgroup.Focus();
        }

        //Lookup Item
        private void TxtName_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                ViewModel.GetItemByName(((TextBox)sender).DataContext);
                si = new SearchItem();
                si.Closed += new EventHandler(si_Closed);
                si.DataContext = ViewModel;
                si.Show();
            }
                
        }

        // Search Item Finished
        void si_Closed(object sender, EventArgs e)
        {
            GridItem.Focus();
        }

 
        //Lookup Inventory
        private void btnfind_Click(object sender, System.Windows.RoutedEventArgs e)
        {
           InventorySearch inventorySearch = new InventorySearch();
           inventorySearch.Closed += new EventHandler(inventorySearch_Closed);
           inventorySearch.Show();
        }


        //Search Inventory Finished
        void inventorySearch_Closed(object sender, EventArgs e)
        {
            var inv = (sender as InventorySearch).Inventory;
            var SoDetail = GridInventory.SelectedItem as SalesOrderDetailDTO;
            SoDetail.Inventory   = inv;
            if (inv != null)
            {
                SoDetail.Panjang = inv.Panjang;
                SoDetail.Lebar = inv.Lebar;
                if (ViewModel.SalesOrder.Warehouse != null && inv.Warehouses.Count > 0)
                    SoDetail.Tinggi = inv.Warehouses.FirstOrDefault(x => x.KodeWarehouse == ViewModel.SalesOrder.Warehouse.KodeWarehouse).Sheet;
                else
                    SoDetail.Tinggi = inv.Tinggi;
                SOW.GetQtyUnit(SoDetail);
            }
            
            GridInventory.Focus();
        }

        //private void ScrollViewer_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        //{
        //    FrameworkElement element = e.OriginalSource as FrameworkElement;
        //    if (element != null)
        //    {
        //        ScrollViewer scrollViewer = sender as ScrollViewer;
        //        scrollViewer.ScrollToHorizontalOffset(GetVerticalOffset(element, scrollViewer));
        //    } 

        //}

        //private double GetVerticalOffset(FrameworkElement child, ScrollViewer scrollViewer)
        //{
        //    GeneralTransform focusedVisualTransform = child.TransformToVisual(scrollViewer);
        //    Point topLeft = focusedVisualTransform.Transform(new Point(child.Margin.Left, child.Margin.Right));
        //    Rect rectangle = new Rect(topLeft, child.RenderSize);
        //    double newOffset = scrollViewer.HorizontalOffset + (rectangle.Right - scrollViewer.ViewportWidth);
        //    return newOffset < 0 ? 0 : newOffset; // no use returning negative offset 
        //}

        private void cboCostCenter_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            //if (SalesOrderViewModel.mode != Mode.Edit)
            //        ViewModel.GetLastNoSalesOrder(cboCostCenter.SelectedValue);
        }

        private void txtNoLog_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

     

        private void KeyDo_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

        private void TxtName_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }



        private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            //SalesOrderDetailDTO soDetail = (sender as TextBox).DataContext as SalesOrderDetailDTO;
            //if (soDetail != null)
            //    if (soDetail.Inventory.Item.IsRounded)
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:N}", soDetail.Qty);
            //    }else
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:0.0000}", soDetail.Qty);
            //    }
        }

        private void txtItemQty_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            SalesOrderDetailDTO soDetail = (sender as TextBox).DataContext as SalesOrderDetailDTO;
            if (soDetail != null)
                if (soDetail.Item.IsRounded)
                {
                    (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:N}", soDetail.Qty);
                }
                else
                {
                    (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:0.0000}", soDetail.Qty);
                }
        }

        private void txtSubQty_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            SalesOrderDetailDTO soDetail = (sender as TextBox).DataContext as SalesOrderDetailDTO;
            if (soDetail != null)
                (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:N}", soDetail.Qty);
               
        }

        private void DatePicker_SelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
        	ViewModel.GetPreSalesList();
        }
    }
}

